package com.api.workflow.web;

import com.api.workflow.util.YNHttpUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.common.util.string.StringUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
 *OA-27.项目立项申请
 * 接口10 内部订单主数据创建接口
 */
public class YNFII003Action extends BaseBean implements Action {
    public static void main(String[] args) {

    }
    @Override
    public String execute(RequestInfo requestInfo){
        //初始化日志信息
        BaseBean baseBean = new BaseBean();
        String lcbh=requestInfo.getWorkflowid();
        int formid=requestInfo.getRequestManager().getFormid();
        //将返回值写为绝对值
        int formidAbs=Math.abs(formid);
        String tableDBName="formtable_main_"+formidAbs;
        //主表数据
        MainTableInfo mainTableInfo=requestInfo.getMainTableInfo();
        Property[] properties=mainTableInfo.getProperty();
        //这里除了主表全部数据 可自行修改存储结构
        Map<String,Object> mainTableInfoMap=new HashMap<>();
        for(int i=0;i<properties.length;i++){
            String name=properties[i].getName().toLowerCase();
            mainTableInfoMap.put(name, Util.null2String(properties[i].getValue()));
        }
        baseBean.writeLog("lyh:OA-21===================mainTableMap"+ com.alibaba.fastjson.JSONObject.toJSONString(mainTableInfoMap));

        //业务逻辑
        try{
            JSONObject map=new JSONObject();
            map.put("ZLCDH",mainTableInfoMap.get("lcbh") );

            map.put("KOKRS",mainTableInfoMap.get("kzfw"));
            map.put("AUART",mainTableInfoMap.get("xmlx"));
            map.put("KTEXT",mainTableInfoMap.get("xmmc"));
            map.put("BUKRS",mainTableInfoMap.get("gsdm"));
            map.put("KOSTV",mainTableInfoMap.get("cbzx"));
            map.put("PRCTR",mainTableInfoMap.get("lrzx"));
            map.put("ZYSJE",mainTableInfoMap.get("xmys"));
            map.put("DATE_WORK_BEGINS",mainTableInfoMap.get("yjksrq"));
            map.put("DATE_WORK_ENDS",mainTableInfoMap.get("yjjsrq"));
            String  xmfzr = String.valueOf(mainTableInfoMap.get("xmfzr"));
            RecordSet recordSet1 = new RecordSet();
            recordSet1.executeQuery("select lastname from hrmResource where id=?",xmfzr);
            recordSet1.next();
            map.put("PERSON_RESP",Util.null2String(recordSet1.getString("lastname")));
            String  sqbm = String.valueOf(mainTableInfoMap.get("sqbm"));
            recordSet1.executeQuery("select departmentcode from hrmdepartment where id=?",sqbm);
            recordSet1.next();
            map.put("DEPARTMENT",Util.null2String(recordSet1.getString("departmentcode")));

            map.put("APPLICATION_DATE",mainTableInfoMap.get("sqrq"));
            String  sqr = String.valueOf(mainTableInfoMap.get("sqr"));
            recordSet1.executeQuery("select lastname from hrmResource where id=?",sqr);
            recordSet1.next();

            map.put("ZNAME",Util.null2String(recordSet1.getString("lastname")));

            JSONObject object = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(map);

            object.put("DATA",jsonArray);

            baseBean.writeLog("lyh:0A-27================object"+object.toString());
            //调用HTTP工具类发送请求
            String post= YNHttpUtils.FII003(object.toString(),YNHttpUtils.token());
            baseBean.writeLog("lyh:0A-27================post"+post);

            JSONObject jsonObject=JSONObject.parseObject(post);

            String STATUS=jsonObject.getString("STATUS");
            String MESSAGE=jsonObject.getString("MESSAGE");

            JSONArray aReturn = jsonObject.getJSONArray("RETURN");
            // 获取第一个元素
            JSONObject firstElement = (JSONObject) aReturn.get(0);

            String message = firstElement.getString("MESSAGE");
            String AUFNR = firstElement.getString("AUFNR");
            if(StringUtil.isNullOrEmpty(AUFNR)){
                AUFNR=null;
            }
            if(StringUtil.isNullOrEmpty(MESSAGE)){
                MESSAGE=message;
            }
            //更新主表qqcs和fhjg
            RecordSet recordSet = new RecordSet();
            String updateMainTableName = "update " + tableDBName + " set qqcsx = ? , fhjgx = ?,nbddh=?  where  requestid = ? ";
            recordSet.executeUpdate(updateMainTableName, object.toString(), post,AUFNR, requestInfo.getRequestid());

            baseBean.writeLog("OA-27========updateMainTableName"+updateMainTableName);

            if (!"S".equals(STATUS)) {
                requestInfo.getRequestManager()
                        .setMessageid(requestInfo.getRequestid()+"OA-27");
                requestInfo.getRequestManager().setMessagecontent("SAP接口异常，请联系SAP人员，错误信息提示:"+MESSAGE);
                return Action.FAILURE_AND_CONTINUE;
            }



        } catch (Exception e) {
            //捕获异常并记录日志
            requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid()+"OA-27");
            requestInfo.getRequestManager().setMessagecontent("OA接口异常"+e.toString());
            return Action.FAILURE_AND_CONTINUE;
        }
        //返回成功状态
        return Action.SUCCESS;
    }
    /**
     * 提取明细表全部的数据
     * @param requestInfo
     * @return
     **/
    private static List<Map<String, Object>> getMaps(RequestInfo requestInfo) {
        DetailTable[] detailTable = requestInfo.getDetailTableInfo().getDetailTable();
        //这里是存储了全部的明细表数据  可自行修改存储结构
        List<Map<String, Object>> detailTableMap = new ArrayList<>();
        if (detailTable.length > 0) {
            for (int i = 0; i < detailTable.length; i++) {
                DetailTable dt = detailTable[i];
                Row[] s = dt.getRow();
                for (int j = 0; j < s.length; j++) {
                    Row r = s[j];
                    Cell c[] = r.getCell();
                    Map<String, Object> objectObjectHashMap = new HashMap<>();
                    for (int k = 0; k < c.length; k++) {
                        Cell c1 = c[k];
                        String name = c1.getName().toLowerCase();
                        String value = c1.getValue();
                        objectObjectHashMap.put(name, value);
                    }
                    detailTableMap.add(objectObjectHashMap);
                }
            }
        }
        return detailTableMap;
    }
}
